	var latlng = new google.maps.LatLng(10.777937,106.70221);
	var options = {
		zoom : 15,
		center : latlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		draggableCursor : "crosshair",
		streetViewControl : false
	};
	var map = new google.maps.Map(document
			.getElementById("map"), options);
	$("#zoom").html(5);
	google.maps.event.addListener(map, "click", function(
			location) {
		GetLocationInfo(location.latLng);
	});
	google.maps.event.addListener(map, 'zoom_changed',
			function(oldLevel, newLevel) {
				$("#zoom").html(map.getZoom());
			});
	var myPano = new google.maps.StreetViewPanorama(document
			.getElementById("pano"), {
		visible : false
	});
	myPano.setPov({
		heading : 265,
		zoom : 1,
		pitch : 0
	});
	$('#pano').hide();
	google.maps.event.trigger(myPano, 'resize');

	// autocomplete
	var autocomplete = new google.maps.places.Autocomplete(
			document.getElementById('postcode'), {});
	google.maps.event.addListener(autocomplete,
			'place_changed', function() {
				$("#locations").html("");
				$("#error").html("");
				var place = autocomplete.getPlace();
				GotoLocation(place.geometry.location);
			});

	var initListener;
	var marker;
	function StartStreetView() {
		// street view
		if ($("#streetViewBtn").val() == "Start StreetView") {
			initListener = google.maps.event.addListener(
					myPano, "position_changed", handlePanoMove);
			$('#pano').show();
			myPano.setVisible(true);
			$("#streetViewBtn").val("End StreetView");
			google.maps.event.trigger(myPano, 'resize');
			GotoLatLong();
		} else {
			google.maps.event.removeListener(initListener);
			myPano.setVisible(false);
			$('#pano').hide();
			$("#streetViewBtn").val("Start StreetView");
			google.maps.event.trigger(myPano, 'resize');
		}
	}

	function Geocode() {
		$("#locations").html("");
		$("#error").html("");
		// geocode with google.maps.Geocoder
		var localSearch = new google.maps.Geocoder();
		var postcode = $("#postcode").val();
		localSearch
				.geocode(
						{
							'address' : postcode
						},
						function(results, status) {
							if (results.length == 1) {
								var result = results[0];
								var location = result.geometry.location;
								GotoLocation(location);
							} else if (results.length > 1) {
								$("#error")
										.html(
												"Multiple addresses found");
								// build a list of possible addresses
								var html = "";
								for ( var i = 0; i < results.length; i++) {
									html += '<a href="javascript:GotoLocation(new google.maps.LatLng('
											+ results[i].geometry.location
													.lat()
											+ ', '
											+ results[i].geometry.location
													.lng()
											+ '))">'
											+ results[i].formatted_address
											+ "</a><br/>";
								}
								$("#locations").html(html);
							} else {
								$("#error").html(
										"Address not found");
							}
						});
	}

	function GotoLocation(location) {
		GetLocationInfo(location);
		map.setCenter(location);
	}

	function GetLocationInfo(latlng) {
		if (latlng != null) {
			ShowLatLong(latlng);
			UpdateStreetView(latlng);
		}
	}

	function GotoLatLong() {
		if ($("#lat").val() != "" && $("#long").val() != "") {
			var lat = $("#lat").val();
			var long = $("#long").val();
			var latLong = new google.maps.LatLng(lat, long);
			ShowLatLong(latLong);
			map.setCenter(latLong);
			UpdateStreetView(latLong);
		}
	}

	function ShowLink() {
		$("#mapLink").html(
				'<a href="https://maps.google.com/?ll='
						+ $("#lat").val() + ','
						+ $("#long").val() + '&z='
						+ $("#zoom").html()
						+ '">Link for this map</a>');
	}

	function toDMS(latOrLng) {
		var d = parseInt(latOrLng);
		var md = Math.abs(latOrLng - d) * 60;
		var m = Math.floor(md);
		var sd = (md - m) * 60;
		return Math.abs(d) + "\u00B0 " + m + "' "
				+ roundNumber(sd, 4) + '"';
	}

	function latToDMS(lat) {
		var dms = toDMS(lat);
		if (lat > 0)
			return dms + "N";
		else
			return dms + "S";
	}

	function lngToDMS(lng) {
		var dms = toDMS(lng);
		if (lng > 0)
			return dms + "E";
		else
			return dms + "W";
	}

	function ShowLatLong(latLong) {
		// show the lat/long
		if (marker != null) {
			marker.setMap(null);
		}
		marker = new google.maps.Marker({
			position : latLong,
			map : map
		});
		$("#lat").val(roundNumber(latLong.lat(), 6));
		$("#long").val(roundNumber(latLong.lng(), 6));
		$("#latDMS").html(latToDMS(latLong.lat()));
		$("#lngDMS").html(lngToDMS(latLong.lng()));
		ShowLink();
		GetElevation(latLong.lat(), latLong.lng(), '#elevation');
		
		$('.address_jp,.address_en').val('');
		var lat = latLong.lat();
		var long = latLong.lng();
		GetAdress(lat, long, '.address_jp,.address_en');
		//ReverseGeocode(lat, long, '#address');

		// UK easting/northing
		if ((latLong.lat() > 49) & (latLong.lat() < 61)
				& (latLong.lng() > -12) & (latLong.lng() < 3)) {
			var ll2w = new LatLng(latLong.lat(), latLong.lng());
			ll2w.WGS84ToOSGB36();
			var os2w = ll2w.toOSRef();
			$('#easting').html(Math.round(os2w.easting));
			$('#northing').html(Math.round(os2w.northing));
			$('#gridRef').html(os2w.toTenFigureString());
		} else {
			$('#easting').html("Not in UK");
			$('#northing').html("Not in UK");
			$('#gridRef').html("Not in UK");
		}
	}

	function UpdateStreetView(latLong) {
		// street view
		if ($("#streetViewBtn").val() == "End StreetView") {
			$("#panoError").html("");
			myPano.setVisible(true);
			myPano.setPosition(latLong);
			// also set via the service API so we know if there is a view available
			var service = new google.maps.StreetViewService();
			service
					.getPanoramaByLocation(
							latLong,
							50,
							function(result, status) {
								if (status != google.maps.StreetViewStatus.OK) {
									$("#panoError")
											.html(
													"No street view available");
									myPano.setVisible(false);
								}
							});
		}
	}

	function handlePanoMove(location) {
		ShowLatLong(myPano.getPosition());
	}
	
	function GetAdress(lat, long, selector){
	    var geocoder = new google.maps.Geocoder();
	    var latLong = new google.maps.LatLng(lat, long);
	    geocoder.geocode({'latLng': latLong}, function(results, status) {
	      if (status == google.maps.GeocoderStatus.OK) {
	        if (results) {
	          var foundAddress = false;
	          for (var i=0; i<results.length; i++) {
	            if ((results[i].types[0] == 'street_address') || (results[i].types[0] == 'route')) {
	            	$(selector).val(results[i].formatted_address);
	              foundAddress = true;
	              break;
	            }
	          }
	          if (!foundAddress) {
	        	  $(selector).val(results[0].formatted_address);
	          }
	        }
	      }
	    });
	  }
